<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/1/27
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>医院类型管理</title>
    <script src=" webjars/jquery/3.5.1/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/manager.css"/>
</head>
<script>
    $(function () {

        /* 显示医院类型管理页面 */
        $(".hosp_type").click(function () {
            $(".msg").css("display", "none");
            $(".hospital_manager").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 显示医院介绍页面 */
        $(".hosp_msg").click(function () {
            $(".msg").css("display", "none");
            $(".intr_info").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 显示科室管理页面 */
        $(".hosp_dept").click(function () {
            $(".msg").css("display", "none");
            $(".dept_manager").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 显示医生类别页面 */
        $(".doc_type").click(function () {
            $(".msg").css("display", "none");
            $(".docType_manager").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 显示医生信息页面 */
        $(".doc_msg").click(function () {
            $(".msg").css("display", "none");
            $(".doc_Info").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 显示排班页面 */
        $(".rost_exp").click(function () {
            $(".msg").css("display", "none");
            $(".doc_duty").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })
        /* 显示药品列表 */
        $(".medicine_msg").click(function () {
            $(".msg").css("display", "none");
            $(".medicine_Info").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })
        /* 显示药品类型 */
        $(".medicine_tp").click(function () {
            $(".msg").css("display", "none");
            $(".medicine_type").css("display", "block");
            $(".titText").css("font-weight", "400");
            $(this).css("font-weight", "600");
        })

        /* 添加 */
        // 隐藏、显示弹框
        $(".insertBtn").click(function () {
            $(".addrow").toggle();
        });

        /* 修改 */
        // 隐藏、显示弹框
        $(".updateBtn").click(function () {
            $(".addrow").toggle();
        });

        // 点击除按钮和弹框之外任意地方隐藏弹窗
        $("body").click(function (e) {
            if (!$(e.target).closest(".insertBtn,.addrow,.updateBtn").length) {
                $(".addrow").hide();
            }
        });

        /* 提交 */
        $(".addrow_submit").click(function () {
            $(".addrow").hide();
        })

        /* 取消 */
        $(".addrow_reset").click(function () {
            $(".addrow").hide();
        })

        /* 添加医院简介 */
        $(".insertIntrBtn").click(function () {
            $(".addInfo").toggle();
        });

        // 点击除按钮和弹框之外任意地方隐藏弹窗
        $("body").click(function (e) {
            if (!$(e.target).closest(".insertIntrBtn,.addInfo,.updateIntrBtn").length) {
                $(".addInfo").hide();
            }
        });

        /* 修改医院简介 */
        $(".updateIntrBtn").click(function () {
            $(".addInfo").toggle();
        });

        /* 新增医生信息 */
        $(".docInsert").click(function () {
            $(".addmsg").toggle();
        });

        // 点击除按钮和弹框之外任意地方隐藏弹窗
        $("body").click(function (e) {
            if (!$(e.target).closest(".docInsert,.addmsg,.docUpdate").length) {
                $(".addmsg").hide();
            }
        });

        /* 修改医生信息 */
        $(".docUpdate").click(function () {
            $(".addmsg").toggle();
        });

        /* 添加排班信息 */
        $(".scheInsert").click(function () {
            $(".sche_info").toggle();
        });

        // 点击除按钮和弹框之外任意地方隐藏弹窗
        $("body").click(function (e) {
            if (!$(e.target).closest(".scheInsert,.sche_infoUl,sche_tab,.scheUpdate").length) {
                $(".sche_info").hide();
            }
        });

        /* 修改医生信息 */
        $(".scheUpdate").click(function () {
            $(".sche_info").toggle();
        });

        /* 添加药品 */
        $(".medInsert").click(function () {
            $(".addmedicine").toggle();
        });

    })

    /**
     * 区分添加或修改的参数
     * 0 添加
     * 1 修改
     */
    let cli = 0;

    /**
     * 添加方法
     */
    function submithospital() {
        // let hospitalType = $('#addfun').serializeArray()
        let htName = $('#htName').val();
        let htStatu = $('#htStatus').val();
        let htStatus = 0;
        if (htStatu === 'unuse') {
            htStatus = 0;
        }
        if (htStatu === 'use') {
            htStatus = 1;
        }
        $.ajax({
            method: "post",
            url: "${pageContext.request.contextPath}/newhospitalTypes",
            dataType: "json",
            data: {
                htName: htName,
                htStatus: htStatus
            },
            success: function (r) {
                window.location.href = "${pageContext.request.contextPath}/hospitalTypes";
            }
        })
    }

    /**
     * 修改方法
     */
    function updatehospital() {
        let htId = null;
        $('.eachhtid').each(function (index, v) {
            let checked = $(v).children().first().children().first().prop(`checked`);
            if (checked) {
                htId = $(v).children().first().children().first().val()
            }
        })
        let htName = $('#htName').val();
        let htStatu = $('#htStatus').val();
        let htStatus = 0;
        if (htStatu === 'unuse') {
            htStatus = 0;
        }
        if (htStatu === 'use') {
            htStatus = 1;
        }
        $.ajax({
            method: "put",
            url: "${pageContext.request.contextPath}/rehospitalTypes",
            dataType: "json",
            data: {
                htId: htId,
                htName: htName,
                htStatus: htStatus
            },
            success: function (r) {
                window.location.href = "${pageContext.request.contextPath}/hospitalTypes";
            }
        })
    }

    /**
     * 添加或删除方法区分
     */
    function choisefun() {
        if (cli == 0) {
            submithospital();
        }
        if (cli == 1) {
            updatehospital();
        }
    }

    /**
     * 删除方法
     */
    function deletebyid() {
        let idList = [];
        $('.eachhtid').each(function (index, v) {
            let checked = $(v).children().first().children().first().prop(`checked`);
            if (checked) {
                idList.push($(v).children().first().children().first().val())
            }
        })
        let idListJson = JSON.stringify(idList);
        $.ajax({
            method: "post",
            url: "${pageContext.request.contextPath}/delhospitalTypes",
            dataType: "json",
            data: {
                idList: idListJson
            },
            success: function (r) {
                window.location.href = "${pageContext.request.contextPath}/hospitalTypes";
            }
        })
    }

    /**
     * 禁用/启用的参数
     * 0 禁用
     * 1 启用
     */
    let htStatus = 0;

    /**
     * 启用/启用
     */
    function startuse() {
        let idList = [];
        $('.eachhtid').each(function (index, v) {
            let checked = $(v).children().first().children().first().prop(`checked`);
            if (checked) {
                idList.push($(v).children().first().children().first().val())
            }
        })
        let idListJson = JSON.stringify(idList);
        $.ajax({
            method: "patch",
            url: "${pageContext.request.contextPath}/usehospitalTypes",
            dataType: "json",
            data: {
                idList: idListJson,
                htStatus: htStatus
            },
            success: function (r) {
                window.location.href = "${pageContext.request.contextPath}/hospitalTypes";
            }
        })
    }
</script>
<style>
    .form-sel {
        margin-left: 20px;
        margin-top: 100px;
    }

    ul {
        list-style: none;
    }

    table {
        margin-left: 20px;
    }
</style>
<body>
<button type="button" class="btn insert_hospital insertBtn addBtn" onclick="cli = 0">添加</button>
<button type="button" class="btn update_hospital updateBtn modBtn" onclick="cli = 1">修改</button>
<button type="button" class="btn delete_hospital delBtn" onclick=deletebyid()>删除</button>
<button type="button" class="btn enable_hospital enaBtn" onclick="htStatus = 1,startuse()">启用</button>
<button type="button" class="btn disable_hospital disBtn" onclick="htStatus = 0,startuse()">禁用</button>
<form action="hospitalTypes" method="get" class="form-sel">
    <input type="text" name="id">
    <input type="submit" value="查询">
</form>
<table>
    <tr>
        <td>编号</td>
        <td>类型名称</td>
        <td>创建时间</td>
        <td>创建人</td>
        <td>修改时间</td>
        <td>修改人</td>
        <td>状态</td>
    </tr>
    <c:forEach items="${hospitalTypes}" var="hospitalType" varStatus="h">
        <tr class="eachhtid">
            <td><input type="checkbox" id="htId" value="${hospitalType.htId}"/>${h.index + 1}</td>
            <td id="${hospitalType.htId}1">${hospitalType.htName}</td>
            <td><fmt:formatDate value="${hospitalType.createTime}" pattern="yyyy-MM-dd"/></td>
            <td>${hospitalType.createMan}</td>
            <td><fmt:formatDate value="${hospitalType.updateTime}" pattern="yyyy-MM-dd"/></td>
            <td>${hospitalType.updateMan}</td>
            <td id="${hospitalType.htId}2">
                <c:choose>
                    <c:when test="${hospitalType.htStatus == 0}">
                        <span>禁用</span>
                    </c:when>
                    <c:otherwise>
                        <span>启用</span>
                    </c:otherwise>
                </c:choose>
            </td>
        </tr>
    </c:forEach>
</table>
<div class="addrow">
    <form id="addfun">
        <ul class="addrowUl1">
            <li>类型名称 <input type="text" class="typeName" placeholder="请输入类型名称" id="htName"/></li>
            <li>状态
                <select id="htStatus">
                    <option value="unuse">禁用</option>
                    <option value="use">启用</option>
                </select>
            </li>
        </ul>

        <button type="button" class="addrow_submit" onclick=choisefun()>提交</button>
        <button type="button" class="addrow_reset">取消</button>
    </form>
</div>
</body>
</html>
